﻿using System;
using System.Collections.Generic;
using System.Text;
using ArlesInterfaces.Editor;
using Arles.src.utils;

namespace Arles.src.controller
{
    public class Invoker : IInvoker
    {
        LimitedStack<ICommand> redo;
        LimitedStack<ICommand> undo;
        IHistory history;

        public Invoker(IHistory history)
        {
            this.history = history;
            redo = new LimitedStack<ICommand>(ConfigFile.Instance.Attributes.UndoLimit);
            undo = new LimitedStack<ICommand>(ConfigFile.Instance.Attributes.UndoLimit);
        }

        public void ExecuteCommand(ICommand newCommand)
        {
            newCommand.Execute();
            undo.Push(newCommand);
            redo.Clear();

            //Draw in History
            history.Draw(undo.elements, redo.elements);
        }

        public void Undo()
        {
            if (undo.Count() > 0)
            {
                undo.Peek().Undo();
                redo.Push(undo.Pop());
            }

            //Draw in History
            history.Draw(undo.elements, redo.elements);
        }

        public void Redo()
        {
            if (redo.Count() > 0)
            {
                redo.Peek().Execute();
                undo.Push(redo.Pop());
            }

            //Draw in History
            history.Draw(undo.elements, redo.elements);
        }

        public string WriteOutput()
        {
            return undo.toString() + " - " + redo.toString();
        }
    }
}
